// 使用 Mock
// import Mock from 'mockjs'
var Mock = require('mockjs')

//制动解体数据
var {carsList} = Mock.mock({
	'carsList|30':[
		{
			"id":"@increment",
			"carName":'C64T-厂修-车体-七次',
			"carTime":'@datetime("yyyy-MM-dd HH:mm")',
			"carNum": /\d{7}/,
			"carType":/[A-Z][0-9][0-9][A-Z]/,
			"state":"制动解体完工",
			"confirm":"未确认",
		}
	],
})
//根据url获取query参数
let getQuery = (url,name) =>{
	console.log(url,name);
	let num = url.indexOf('?')
	if(num !== -1) {
	 let queryStrArr =	url.substr(num+1).split('&')
	 // console.log(queryStrArr);
	 for(var i=0 ;i<queryStrArr.length;i++) {
		 let itemArr = queryStrArr[i].split('=')
		 console.log(itemArr);
		 if(itemArr[0] === name) {
			 return itemArr[1]
		 }
	 }
	}
	return null
}

Mock.mock(/\/api\/news/,'get',(options)=>{
	//获取传递的参数pageNum 
	let pageNum = getQuery(options.url,'pageNum')
	//获取传递的参数pageSize
	let pageSize = getQuery(options.url,'pageSize')
	// console.log(pageNum);
	//截取数据的起始位置
	let start = (pageNum-1)*pageSize
	//截取数据的结束位置
	let end = pageNum*pageSize
	//计算总页数
	let totalPage = Math.ceil(carsList.length/pageSize)
	
	//1 0-9
	//2 10-19
	//3 20-29
	//数据的起始位置（pageNum-1）*pageSize 数据的结束位置 pageNum*pageSize
	let list =  pageNum>totalPage?[]:carsList.slice(start,end)
	return {
		status:200,
		msg:"获取首页列表",
		list:list,
		total:carsList.length
	}
})